﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication4;
using System.Threading;
using ZF.BLL;
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Drawing;
using System.Text.RegularExpressions;
using gather;
using System.Resources;
using GCELibrary.Current;
using ZF.BLL;
using System.Threading;
using System.Diagnostics;
using ZF.BLL.Gather;
namespace gather
{
    public class JXParam//
    {
        public string Url;
        public string Html;
        public SimpleTree<int> City;
        public int Status = 0;//0未开始，1进行中，2超时或错误 3完成
        public DateTime BT;
        public DateTime ET;
        public Thread Thread;
        public HouseGatherModel HouseModel;
        public string Domain;

    }
    public abstract class gbase
    {
        public bool GatherListing { get; set; }
        public bool GatherInfoing { get; set; }
        public List<JXParam> JXList = new List<JXParam>();
        public List<JXParam> JXListFinish = new List<JXParam>();
        public List<JXParam> CheckTimeBuff = new List<JXParam>();
        public List<string> errHTML = new List<string>();

       protected object lockJS = new object();
       protected int cRunCount = 0;
        public void AddCheckTimeOut(JXParam jx)
        {
            lock (CheckTimeBuff)
            {
                jx.Thread = Thread.CurrentThread;
                jx.BT = DateTime.Now;
                CheckTimeBuff.Add(jx);
            }

        }
        public void RemoveCheckTimeOut(JXParam jx)
        {
            lock (CheckTimeBuff)
            {
                var item = CheckTimeBuff.FirstOrDefault(p => jx == p);
                if (item != null)
                    CheckTimeBuff.Remove(item);
            }

        }
        public void CheckTimeOutThread(object o)
        {
            while (true)
            {

                List<JXParam> stops = new List<JXParam>();
                lock (CheckTimeBuff)
                {
                    stops.AddRange(CheckTimeBuff.Where(p => p.BT.AddSeconds(1) < DateTime.Now));
                }
                stops.ForEach(p =>
                {
                    errHTML.Add(p.Html);
                    p.Status = 2;
                    p.Thread.Abort();
                    lock (lockJS) cRunCount--;
                    Console.WriteLine(p.Url);
                    RemoveCheckTimeOut(p);
                    Console.WriteLine("------------------------------------------------------");
                }
                    );
                Thread.Sleep(100);
            }
        }

        public void Print(object o)
        {
            var t = o.GetType();
            var fs = t.GetFields();
            foreach (var f in fs)
            {
                Console.WriteLine(string.Format("{0}:{1}", f.Name, string.Concat(f.GetValue(o))));
            }
            Console.WriteLine("====================================================================================");

        }

        public string[] getInfoHTML(string[] urls)
        {
            HttpClient hc = new HttpClient();
            List<string> result = new List<string>();
            foreach (var url in urls)
            {
                string html = null;
                try
                {
                    html = hc.GetSrc(url);
                    result.Add(html);
                }
                catch (Exception)
                {
                }

            }
            return result.ToArray();
        }
        public abstract void Start();
        public List<SimpleTree<int>> cityTree = File.ReadAllText("city.txt").JsonConvert<List<SimpleTree<int>>>();
        public Dictionary<string, SimpleTree<int>> getCityTemplate()
        {
            var allCityList = @"
gz-广州市
sz-深圳市
          dg-东莞市
          zhuhai-珠海市
          shantou-汕头市
          foshan-佛山市
          jiangmen-江门市
          zhongshan-中山市
          huizhou-惠州市
          maoming-茂名市
          shaoguan-韶关市
          zhanjiang-湛江市
          zhaoqing-肇庆市
          meizhou-梅州市
          shanwei-汕尾市
          heyuan-河源市
          yangjiang-阳江市
          qingyuan-清远市
          chaozhou-潮州市
          jieyang-揭阳市
          yunfu-云浮市".Replace(" ", "").Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
                   .Select(p => new Keyword<string>() { Key = p.Split('-')[0], Text = p.Split('-')[1] }).ToList();
            Dictionary<string, SimpleTree<int>> dic = new Dictionary<string, SimpleTree<int>>();
            foreach (var c in allCityList)
            {
                var ct = cityTree.FirstOrDefault(p => p.Name == c.Text);
                if (ct != null)
                    dic[c.Key] = ct;
                else
                {
                    throw new Exception();
                }
            }

            return dic;

        }
    }
    public class main
    {
        public static void Main1(string[] args)
        {


           var  gs = new gbase[] { new cf(), new sp(), new cz(), new es(), new xjl() }.ToList();

            
            gs.ForEach(p =>
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(new Action<object>(a => p.Start())));
                });
            Thread.Sleep(1000);
            while (true)
            {

                if (gs.All(p => p.GatherInfoing == false && p.GatherListing == false))
                {
                    Console.WriteLine("-----------------------操作完成------------------------");
                }
                else
                {
                    Thread.Sleep(1000);
                }
         
            }
            //sp.Main1(null);
            //cf.Main1(null);
            //cz.Main1(null);
            //es.Main1(null);
            //xjl.Main1(null);

        }
    }
}
